<?php

App::uses('Crud', 'Model');
App::uses('UserLine', 'Model');
App::uses('Workstation', 'Model');
App::uses('Area', 'Model');
App::uses('Moe', 'Model');

class Line extends Crud
{
    /** Al Agregar/eliminar/actualizar las constantes de los modelos implica ir
     * y actualizar los comentarios en los campos de la tabla asociada a este modelo.
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;
    const TYPE_REAL = 1;
    const TYPE_VIRTUAL = 2;

    public static function getTypes() {
        return array(
            array('value' => self::TYPE_REAL, 'text' => 'Real'),
            array('value' => self::TYPE_VIRTUAL, 'text' => 'Virtual'),
        );
    }

    /**
     * Obtenemos los registros para el grid del admin
     * @return type
     */
    public function getEnabled() {
        $filters = array(
            'fields' => array(
                'name as text',
                'id as value',
                'area_id',
                'type',
            ),
            'conditions' => array(
                'status' => self::STATUS_ENABLED
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    /**
     * Buscamos un area por su nombre
     * @param string $name
     * @return array
     */
    public function getByName($name) {
        return $this->find('first', array('conditions' => array(
                        'name' => $name
        )));
    }

    /**
     * Obtenemos las lineas por area. Solo obtiene el Id y el Nombre
     * @param type $areaId
     * @return type
     */
    public function getEnabledByAreaBasic($areaId) {
        $filters = array(
            'fields' => array(
                'Line.id as value',
                'Line.name as text',
            ),
            'conditions' => array(
                'Line.status' => self::STATUS_ENABLED,
                'Line.type' => self::TYPE_VIRTUAL,
                'Line.area_id' => $areaId,
            ),
            'group' => array(
                'Line.id',
            ),
            'order' => array(
                'Line.name' => 'ASC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }
    
    /**
     * Obtenemos las lineas por area
     * @param type $areaId
     * @return type
     */
    public function getEnabledByArea($areaId) {
        $filters = array(
            'fields' => array(
                'Line.id as value',
                'Line.name as text',
                'Line.area_id',
                'Line.type',
                'Line.colour',
                'count(w.id) as num_workstations'
            ),
            'joins' => array(
                array(
                    'table' => 'workstations',
                    'alias' => 'w',
                    'type' => 'left',
                    'conditions' => array(
                        'w.status' => Workstation::STATUS_ENABLED,
                        'w.line_id = Line.id',
                    ),
                ),
            ), 
            'conditions' => array(
                'Line.status' => self::STATUS_ENABLED,
                'Line.area_id' => $areaId,
            ),
            'group' => array(
                'Line.id',
            ),
            'order' => array(
                'Line.type' => 'DESC',
                'Line.name' => 'ASC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    /**
     * Obtenemos lineas por area y tipo
     * @param string $areaId UUID
     * @param integer $type
     * @return array
     */
    public function getEnabledByAreaAndType($areaId, $type) {
        $filters = array(
            'fields' => array(
                'id as value',
                'name as text',
                'colour',
            ),
            'conditions' => array(
                'status' => self::STATUS_ENABLED,
                'area_id' => $areaId,
                'type' => $type,
            ),
            'order' => array(
                'name' => 'ASC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    /**
     * Obtenemos las lineas de un area que no tienen registro de UserLine
     * @param type $areaId
     * @param type $userId
     * @return type
     */
    public function getLinesWithoutUserLine($areaId, $userId) {
        $filters = array(
            'fields' => array(
                'id',
            ),
            'joins' => array(
                array(
                    'table' => 'user_lines',
                    'alias' => 'ul',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'ul.status' => UserLine::STATUS_ENABLED,
                        'ul.line_id = Line.id',
                        'ul.user_id' => $userId,
                    ),
                ),
            ),
            'conditions' => array(
                'Line.status' => self::STATUS_ENABLED,
                'area_id' => $areaId,
                'ul.id IS NULL',
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    /**
     * Obtenemos las lineas por area
     * @param type $areaId
     * @return type
     */
    public function getEnabledWithHierarchy() {
        $filters = array(
            'fields' => array(
                'Line.id as value',
                'Line.name as text',
                'Line.area_id',
                'm.id as moe_id',
            ),
            'joins' => array(
                array(
                    'table' => 'areas',
                    'alias' => 'a',
                    'type' => 'inner',
                    'conditions' => array(
                        'a.id = Line.area_id',
                    ),
                ),
                array(
                    'table' => 'moes',
                    'alias' => 'm',
                    'type' => 'inner',
                    'conditions' => array(
                        'm.id = a.moe_id',
                    ),
                ),
            ), 
            'conditions' => array(
                'Line.status' => self::STATUS_ENABLED,
                'Line.type' => self::TYPE_VIRTUAL,
            ),
            'group' => array(
                'Line.id',
            ),
            'order' => array(
                'Line.name' => 'ASC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    /**
     * Obtenemos las lineas que estan activas y que el area esta activa y que el moe esta activo
     * @return array
     */
    public function getFullEnabled()
    {
        $filters = array(
            'fields' => array(
                'Line.id as value',
                'CONCAT(m.name, " - ", a.name, " - ",Line.name) as text',
            ),
            'joins' => array(
                array(
                    'table' => 'areas',
                    'alias' => 'a',
                    'type' => 'inner',
                    'conditions' => array(
                        'a.id = Line.area_id',
                        'a.status' => Area::STATUS_ENABLED,
                    ),
                ),
                array(
                    'table' => 'moes',
                    'alias' => 'm',
                    'type' => 'inner',
                    'conditions' => array(
                        'm.id = a.moe_id',
                        'm.status' => Moe::STATUS_ENABLED,
                    ),
                ),
            ),
            'conditions' => array(
                'Line.status' => self::STATUS_ENABLED,
                'Line.type' => self::TYPE_REAL,
            ),
            'order' => array(
                'm.name ASC',
                'a.name ASC',
                'Line.name' => 'ASC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    /**
     * Obtenemos las lineas que estan activas y que el area esta activa y que el moe esta activo
     * @return array
     */
    public function getFullEnabledShortName()
    {
        $filters = array(
            'fields' => array(
                'Line.id as value',
                'Line.name as text',
            ),
            'joins' => array(
                array(
                    'table' => 'areas',
                    'alias' => 'a',
                    'type' => 'inner',
                    'conditions' => array(
                        'a.id = Line.area_id',
                        'a.status' => Area::STATUS_ENABLED,
                    ),
                ),
                array(
                    'table' => 'moes',
                    'alias' => 'm',
                    'type' => 'inner',
                    'conditions' => array(
                        'm.id = a.moe_id',
                        'm.status' => Moe::STATUS_ENABLED,
                    ),
                ),
            ),
            'conditions' => array(
                'Line.status' => self::STATUS_ENABLED,
                'Line.type' => self::TYPE_REAL,
            ),
            'order' => array(
                'm.name ASC',
                'a.name ASC',
                'Line.name' => 'ASC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }
}
